package com.cloudptt.wp.ui.menu;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.BaseActivity;
import com.cloudptt.wp.ui.UiDlg;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.model.AddrInfo;

import java.util.List;
import java.util.Vector;

public class GroupAddressActivity extends BaseActivity implements DialogInterface.OnClickListener{

	private final static String TAG = "GroupAddressActivity";
	private TextView activity_title;
	private ListAdapter mAdapter;
	private ListView mListView;
	private int curPos = 0;
	private List<AddrInfo> list = new Vector<AddrInfo>();
	private McIdInfo mcIdInfo = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Window window = getWindow();
		window.addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_layout);

		initView();
		updateListDataByeType();
		ScreenManager.getScreenManager().pushActivity(this);
	}

	private void updateListDataByeType()
	{
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		mcIdInfo = product.getMcpttLoginInfo().getMcIdInfo();
		AddrOperator op = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
		list = op.getGroupAddrList();
		Log.d(TAG, "updateListDataByeType list "+ list);
		mAdapter.notifyDataSetChanged();
		mListView.requestFocus();
		mListView.setSelection(0);


	}

	private void initView() {
		activity_title =  (TextView) findViewById(R.id.activity_title);
		activity_title.setText(R.string.group);
		mListView = (ListView) findViewById(R.id.list);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				curPos = mListView.getSelectedItemPosition();
				showDlg();
			}
		});
		mAdapter = new ListAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setSelection(0);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(TAG, "onKeyDown keyCode "+ keyCode);

		if (keyCode == KeyEvent.KEYCODE_SOFT_LEFT
				|| keyCode == 66)
		{
			curPos = mListView.getSelectedItemPosition();
			showDlg();
			return false;
		}
//		else if (keyCode == KeyEvent.KEYCODE_BACK) {
//			//gotoSettingActivity();
//			return false;
//		}

		if (keyCode == 19)//xia
		{
			curPos = mListView.getSelectedItemPosition();
			Log.d(TAG, "onKeyDown 19 curPos "+ curPos);
			if(curPos <= 0)
			{
				curPos = list.size() -1 ;
			}
			Log.d(TAG, "onKeyDown 19 after curPos "+ curPos);
			mListView.setSelection(curPos);
			return true;
		}
		else if (keyCode == 20) //shang
		{
			curPos = mListView.getSelectedItemPosition();
			Log.d(TAG, "onKeyDown 20 curPos "+ curPos);
			if(curPos >= (list.size()-1))
			{
				curPos = 0;
			}
			Log.d(TAG, "onKeyDown 20 after curPos "+ curPos);
			mListView.setSelection(curPos);
			return true;
		}
		else if (keyCode == 21)
		{
			return true;
		}
		else if (keyCode == 22)
		{
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public void onClick(DialogInterface arg0, int index) {
		// TODO Auto-generated method stub
		AddrInfo info = list.get(curPos);
		if(index == 0)
		{
//			if(curListType == TYPE_GROUP)
//			{
			setDefaultGroup(info);
//			}
//			else
//			{
//				doClickCallBtnAction(info);
//			}
		}
	}
	private void showDlg()
	{
		String item1 = getString(R.string.option_call);
		item1 = getString(R.string.option_gcall_default);

		String[] list = new String[]{item1};//new String[]{item1,item2,item3,item4};
		UiDlg.showListDialog(GroupAddressActivity.this, list, this);
	}
	private void setDefaultGroup(AddrInfo info)
	{
		Log.debug(AddressActivity.class, "setDefaultGroup info = "+info);
		if(info != null)
		{
			SysApplication app = ((SysApplication) getApplicationContext());
			Product product = app.getProduct();
			if(product!=null)
			{
				product.setDefaultGroupInfo(info);//.setCurGroupInfo(info);
				product.setCurGroupInfo(null);
				if(mAdapter != null)
				{
					mAdapter.notifyDataSetChanged();
				}
			}
		}
	}
	private AddrInfo getDefaultGroup()
	{
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		if(product!=null)
		{
			return product.getDefaultGroupInfo();
		}
		return null;
	}



	@Override
	protected void onDestroy(){
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	class ListAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder viewHolder = new ViewHolder();

			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.item_layout, parent, false);
				viewHolder.icon = (ImageView)convertView.findViewById(R.id.settings_item_icon);
				viewHolder.title = (TextView)convertView.findViewById(R.id.settings_item_title);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}

			AddrInfo info = list.get(position);
			Log.d(TAG, "getView info "+ info);

//			if(info.isUserType())
//			{
//				UserInfo uInfo = (UserInfo)info;
//				viewHolder.title.setText(info.getDscp());
//				viewHolder.icon.setImageResource(R.drawable.user_offline);
//				if(uInfo.getOnlineState() == 1)
//				{
//					viewHolder.icon.setImageResource(R.drawable.user_online);
//				}
//			}
//			else
//			{
				AddrInfo defaultGInfo = getDefaultGroup();
				if(info.isSameOne(defaultGInfo))
				{
					String str = getString(R.string.ginfo_default);
					viewHolder.title.setText(info.getDscp() + "          " + str);
				}
				else
				{
					viewHolder.title.setText(info.getDscp());
				}
				viewHolder.icon.setVisibility(View.GONE);
//			}
			return convertView;
		}
	}

	class ViewHolder{
		public ImageView icon;
		public TextView title;
	}


}
